package com.suqiu.qqrobot.service.impl;


import com.suqiu.qqrobot.common.log.annotation.LogMgr;
import com.suqiu.qqrobot.config.OpenAiChatContext;
import com.suqiu.qqrobot.model.Message;
import com.suqiu.qqrobot.model.TextMessage;
import com.unfbx.chatgpt.OpenAiClient;
import com.unfbx.chatgpt.entity.chat.ChatCompletion;
import com.unfbx.chatgpt.entity.chat.ChatCompletionResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@LogMgr
@Slf4j
@RequiredArgsConstructor
@Service("openAiTurboModelService")
public class OpenAiTurboModelService implements OpenAiChatService<String, String> {

    private final OpenAiClient openAiClient;
    private final OpenAiChatContext openAiChatContext;

    @Override
    public Message<String> chat(String sessionId, String question) {
        //聊天模型：gpt-3.5-turbo
        ArrayList<com.unfbx.chatgpt.entity.chat.Message> messages = getMessages(openAiChatContext, sessionId, question);

//        ChatCompletion chatCompletion = ChatCompletion.builder()
//                .maxTokens(openAiProperties.getChatGpt().getMaxToken())
//                .temperature(openAiProperties.getChatGpt().getTemperature())
//                .topP(openAiProperties.getChatGpt().getTopP())
//                .messages(messages)
//                .build();

        //zero model 可以指定gpt ,参数默认gpt-3.5-turbo @see com.unfbx.chatgpt.entity.chat.ChatCompletion.Model.GPT_3_5_TURBO
        //.model(ChatCompletion.Model.GPT_3_5_TURBO_0301.getName())
        ChatCompletion chatCompletion = ChatCompletion.builder()
                .model(ChatCompletion.Model.GPT_3_5_TURBO.getName())
                .messages(messages).build();

        ChatCompletionResponse chatCompletionResponse = openAiClient.chatCompletion(chatCompletion);
        Message<String> message = new TextMessage();
        message.setMessage(chatCompletionResponse.getChoices().get(0).getMessage().getContent());
        return message;
    }
}
